package gui

import (
	"context"
	"gitee.com/xgh2012/wails_common/wails_core"
	"gitee.com/xgh2012/wails_common/wails_core/systems"
	"github.com/wailsapp/wails/v2/pkg/runtime"
)

type Dialog struct {
	Ctx context.Context
}

// OpenDirectoryDialog 选择文件夹
func (a *Dialog) OpenDirectoryDialog(title string) *wails_core.Result {
	folder, err := runtime.OpenDirectoryDialog(systems.Context, runtime.OpenDialogOptions{
		Title: title,
	})
	if err != nil {
		return wails_core.Wrong(1001, err.Error(), nil)
	}
	return wails_core.Success(folder)
}

// OpenFileDialog  选择文件
func (a *Dialog) OpenFileDialog(title string, fitler string) (string, error) {
	//Filters:= []runtime.FileFilter{
	//	{
	//		DisplayName: "Images (*.png;*.jpg)",
	//		Pattern:     "*.png;*.jpg",
	//	}, {
	//		DisplayName: "Videos (*.mov;*.mp4)",
	//		Pattern:     "*.mov;*.mp4",
	//	},
	//},
	op := runtime.OpenDialogOptions{
		Title: title,
	}
	return runtime.OpenFileDialog(systems.Context, op)
}

// InfoDialog 系统提示错误
func (a *Dialog) InfoDialog(message string) (string, error) {
	result, err := runtime.MessageDialog(systems.Context, runtime.MessageDialogOptions{
		Type:          runtime.InfoDialog,
		Title:         "系统信息",
		Message:       message,
		DefaultButton: "No",
	})
	return result, err
}

// WaringDialog 系统提示错误
func (a *Dialog) WaringDialog(message string) (string, error) {
	result, err := runtime.MessageDialog(systems.Context, runtime.MessageDialogOptions{
		Type:          runtime.WarningDialog,
		Title:         "系统提示",
		Message:       message,
		DefaultButton: "No",
	})
	return result, err
}

// ErrorDialog 系统提示错误
func (a *Dialog) ErrorDialog(message string) {
	_, _ = runtime.MessageDialog(systems.Context, runtime.MessageDialogOptions{
		Type:    runtime.ErrorDialog,
		Title:   "系统错误",
		Message: message,
	})
}

// ErrorDialogWithExist 系统提示错误，并退出
func (a *Dialog) ErrorDialogWithExist(message string) {
	_, _ = runtime.MessageDialog(systems.Context, runtime.MessageDialogOptions{
		Type:    runtime.ErrorDialog,
		Title:   "系统错误",
		Message: message,
	})
	runtime.Quit(systems.Context)
}

// QuestionDialog 系统问题提示
func (a *Dialog) QuestionDialog(message string) (string, error) {
	result, err := runtime.MessageDialog(systems.Context, runtime.MessageDialogOptions{
		Type:          runtime.QuestionDialog,
		Title:         "系统问题",
		Message:       message,
		DefaultButton: "No",
		CancelButton:  "Cancel",
	})
	return result, err
}
